package com.luoluo.view;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ViewServlet extends HttpServlet {
	private static final long serialVersionUID = 1369236791239786599L;
	private static final String WWW = "/WEB-INF/www";
	private static final String VM = ".vm";
	private static final String INDEX = WWW + "/" + "index" + VM;

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String uri = request.getRequestURI();
		if (uri.endsWith("/"))
			uri = uri.substring(0, uri.lastIndexOf("/"));
		if (uri.endsWith(".html"))
			uri = uri.substring(0, uri.lastIndexOf(".html"));
		if (uri.endsWith(".htm"))
			uri = uri.substring(0, uri.lastIndexOf(".htm"));
		if (uri.endsWith(".vm"))
			return;
		if (uri.endsWith(".love"))
			return;
		String queryString = request.getQueryString();
		if (null == uri || uri.length() == 0) {
			uri = INDEX + (null == queryString ? "" : "?" + queryString);
		} else {
			uri = WWW + uri + VM
					+ (null == queryString ? "" : "?" + queryString);
		}
		System.out.println(uri);
		request.getRequestDispatcher(uri).forward(request, response);
	}
}
